<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1100px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class">
            <el-form-item label="广告ID" prop="advertiser_id">
                <el-input v-model.trim="model.advertiser_id" placeholder="请输入广告ID" clearable></el-input>
            </el-form-item>
            <!-- TODO改成api拉取 -->
            <el-form-item label="广告账户" prop="info">
                <el-input v-model.trim="model.info" placeholder="请输入广告账户" clearable></el-input>
            </el-form-item>
            <!-- TODO改成下拉框 -->
            <el-form-item label="开户主体" prop="advertiser_name">
                <!-- <el-input v-model.trim="model.advertiser_name" placeholder="请输入开户主体" clearable></el-input> -->
                <el-select v-model="model.advertiser_name" placeholder="请选择开户主体" clearable>
                    <el-option v-for="(item, index) in dict.advertiser_namelist" :key="index" :label="item.title" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="平台" prop="platform_id">
                <el-select v-model="model.platform_id" placeholder="请选择平台" clearable>
                    <el-option v-for="(item, index) in dict.platformList" :key="index" :label="item.title" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="代理归属" prop="ProxyOwnership">
                <el-select v-model="model.ProxyOwnership" placeholder="请选择代理归属" clearable>
                    <el-option v-for="(item, index) in dict.ProxyOwnershipList" :key="index" :label="item.title" :value="item.id"></el-option>
                </el-select>
                <!-- <SelectPage v-model="model.ProxyOwnership" placeholder="请选择代理归属" :url="$api.advertisement.proxyownershipList" show_field="name" query_field="name" query_field2="id" filterable clearable popper-class="custom-select"></SelectPage> -->
            </el-form-item>
            <!-- <el-form-item label="分配给运营" prop="DoYouWantToModifyIt">
                <el-switch v-model="model.DoYouWantToModifyIt" :active-value="1" :inactive-value="0" active-text="开启分配" inactive-text="暂不分配"></el-switch>
            </el-form-item> -->
            <!-- <el-form-item label="店铺" prop="shop_id" v-if="model.DoYouWantToModifyIt" >
                <el-select v-model="model.shop_id" placeholder="请选择店铺" clearable size="small">
                    <el-option v-for="(item, index) in dict.shopList" :key="index" :label="item.shop_name" :value="item.shop_id" />
                </el-select>
            </el-form-item> -->

            <!-- TODO改成模糊搜素 在职的放前面 离职放后面 显示在职离职状态-->
            <!-- <el-form-item label="运营" prop="admin_id" v-if="model.DoYouWantToModifyIt" >
                <el-select v-model="model.admin_id" placeholder="请选择运营" clearable size="small" style="width: 240px">
                    <el-option v-for="(item, index) in dict.salesmanList" :key="index" :label="item.nickname" :value="item.id" />
                </el-select>
            </el-form-item> -->
            <!-- <el-form-item label="平台" prop="platform_id" v-if="model.DoYouWantToModifyIt" >
                <el-select v-model="model.platform_id" placeholder="请选择平台" clearable>
                    <el-option v-for="(item, index) in dict.platformList" :key="index" :label="item.title" :value="item.id"></el-option>
                </el-select>
            </el-form-item> -->
            <!-- <el-form-item label="API状态" prop="status">
                <el-switch v-model="model.status" :active-value="1" :inactive-value="0" active-text="正常" inactive-text="关闭"></el-switch>
            </el-form-item> -->
            <!-- TODO改成动态增减表单 -->
            <!-- <el-form-item label="接手日期" prop="createtime" v-if="model.DoYouWantToModifyIt" >
                <el-date-picker v-model="model.createtime" type="date" placeholder="请选择开始时间" clearable style="width: 240px"> </el-date-picker>
            </el-form-item> -->
            <!-- <el-form-item label="结束时间" prop="updatetime" v-if="model.DoYouWantToModifyIt" >
                <el-date-picker v-model="model.updatetime" type="date" placeholder="请选择结束时间" clearable style="width: 240px"> </el-date-picker>
            </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { accountopeningList, proxyownershipList} from '@/api/advertisement'
import { getShopList, getSalesmanList } from '@/api/shop'
import { queryAdvplatformList, queryAdvaccountDetail, advaccountAdd, advaccountEdit } from '@/api/advertisement'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            model: {
                id: '',
                advertiser_id: '',
                info: '',
                DoYouWantToModifyIt: 0,
                advertiser_name: '',
                admin_id: '',
                shop_id: '',
                platform_id: '',
                status: 1,
                createtime: '',
                updatetime: '',
            },
            rules: {
                advertiser_id: [{ required: true, message: '广告账户不能为空', trigger: ['blur', 'change'] }],
                status: [{ required: true, message: '状态不能为空', trigger: ['blur', 'change'] }],
            },
            dict: {
                platformList: [],
                shopList: [],
                salesmanList: [],
                advertiser_namelist: [],
                ProxyOwnershipList: [],
            },
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}广告账户`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async mounted() {
        try {
            const [platformData, shopData, salesmanData,advertiser_nameData,ProxyOwnershipData] = await Promise.all([queryAdvplatformList(), getShopList(), getSalesmanList(), accountopeningList(), proxyownershipList()])
            console.log(platformData)
            this.dict.platformList = platformData?.data?.list ?? []
            this.dict.shopList = shopData?.data?.list ?? []
            this.dict.salesmanList = salesmanData?.data?.list ?? []
            this.dict.advertiser_namelist = advertiser_nameData?.data?.list ?? []
            this.dict.ProxyOwnershipList = ProxyOwnershipData?.data?.list ?? []
        } catch (error) {
            console.log(error)
        }
        
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            queryAdvaccountDetail(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                    // this.model.status = res.status === 'normal' ? 1 : 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    advaccountEdit(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    advaccountAdd(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 50%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 250px;
}
.form-class.el-form .el-form-item .el-textarea {
    width: 770px;
}
.form-class.el-form .el-form-item .el-input-number,
.form-class.el-form .el-form-item .el-input-number .el-input {
    width: 120px;
}
.form-class.el-form .el-form-item .el-input-number + .el-input-number {
    margin-left: 10px;
}
.form-class.el-form .el-form-item.full {
    width: 100%;
}
.upload {
}
.upload-tip span {
    color: #ff4949;
    padding: 0 4px;
}
</style>
